package com.ysd.lis.service.bac.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.bac.BacReqPurpose;
import com.ysd.lis.entity.bac.BacReqPurposeFeeItem;
import com.ysd.lis.entity.lab.LabFeeItem;
import com.ysd.lis.entity.lab.LabSampleType;
import com.ysd.lis.mapper.bac.BacReqPurposeFeeItemMapper;
import com.ysd.lis.request.bac.PurposeDto;
import com.ysd.lis.request.bac.PurposeItemParam;
import com.ysd.lis.service.bac.BacReqPurposeFeeItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-04-11
 */
@Service
public class BacReqPurposeFeeItemServiceImpl extends ServiceImpl<BacReqPurposeFeeItemMapper, BacReqPurposeFeeItem> implements BacReqPurposeFeeItemService {

    @Autowired
    BacReqPurposeFeeItemMapper bacReqPurposeFeeItemMapper;

    @Override
    public Result findPurposeFeeItemList(PurposeItemParam param) {

        MPJLambdaWrapper<BacReqPurposeFeeItem> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(BacReqPurposeFeeItem.class);
        queryWrapper.selectAs("pu", BacReqPurpose::getPurposeName, BacReqPurposeFeeItem::getPurposeName);
        queryWrapper.selectAs("sa", LabSampleType::getSampleTypeName, BacReqPurposeFeeItem::getSampleTypeName);
        queryWrapper.selectAs("fe", LabFeeItem::getFeeName, BacReqPurposeFeeItem::getFeeItemName);
        queryWrapper.leftJoin(BacReqPurpose.class, "pu", p -> p.eq(BacReqPurpose::getPurposeNo, BacReqPurposeFeeItem::getPurposeNo).eq(BacReqPurpose::getDelFlag, 0));
        queryWrapper.leftJoin(LabSampleType.class, "sa", p -> p.eq(LabSampleType::getSampleTypeCode, BacReqPurposeFeeItem::getSampleTypeCode).eq(LabSampleType::getDelFlag, 0));
        queryWrapper.leftJoin(LabFeeItem.class, "fe", p -> p.eq(LabFeeItem::getFeeCode, BacReqPurposeFeeItem::getFeeCode).eq(LabFeeItem::getDelFlag, 0));
        queryWrapper.eq(BacReqPurposeFeeItem::getDelFlag, 0);
        queryWrapper.and(p -> p.eq(BacReqPurposeFeeItem::getPurposeNo, param.getPurposeNo()).eq(BacReqPurposeFeeItem::getDelFlag, 0));
        queryWrapper.and(p -> p.eq(BacReqPurposeFeeItem::getSampleTypeCode, param.getSampleTypeId()).eq(BacReqPurposeFeeItem::getDelFlag, 0));
        queryWrapper.orderByAsc(BacReqPurposeFeeItem::getSeq);
        List<BacReqPurposeFeeItem> detailList = bacReqPurposeFeeItemMapper.selectList(queryWrapper);

        return Result.succ(1, "查询成功", detailList);

    }

    @Override
    public Result addPurposeFeeItem(BacReqPurposeFeeItem bacReqPurposeFeeItem) {
        int insert = bacReqPurposeFeeItemMapper.insert(bacReqPurposeFeeItem);
        if (insert > 0) {
            return Result.succ(1, "添加成功", bacReqPurposeFeeItem);
        } else {
            return Result.fail(400, "添加失败", "");
        }
    }

    @Override
    public Result batchDeletePurposeItemList(PurposeDto dto) {
        List<BacReqPurposeFeeItem> bacReqPurposeFeeItemList = dto.getBacReqPurposeFeeItemList();
        List<String> ids = bacReqPurposeFeeItemList.stream().map(BacReqPurposeFeeItem::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);

        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }

    @Override
    public Result deletePurposeFeeItem(BacReqPurposeFeeItem bacReqPurposeFeeItem) {
        int deleteById = bacReqPurposeFeeItemMapper.deleteById(bacReqPurposeFeeItem.getId());
        if (deleteById > 0) {
            return Result.succ(1, "删除成功", "");
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }
}
